package m.base.action;

import m.base.model.AppInfo;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelUpdateUtil;
import m.html.VueFormResult;
import m.html.form.*;
import m.system.cache.CacheUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "base/appInfo")
public class AppInfoAction extends BaseAction{
    private AppInfo model;
    @RequestMeta(logTitle = "APP信息",logDescription = "保存APP信息")
    public JSONMessage doSave() throws Exception {
        PowerUtil.verifyPower(PowerUtil.SysData);//权限判断
        ModelUpdateUtil.updateModel(model);
        CacheUtil.clear(AppInfo.class);
        return new JSONMessage(0,"修改成功");
    }
    @RequestMeta(params = {"registerCode","phone"},logTitle = "APP信息",logDescription = "生成注册码")
    public JSONMessage doBuild(String registerCode,String phone) throws Exception {
        PowerUtil.verifyPower(PowerUtil.SysData);//权限判断
        return new JSONMessage(0).push("codeNumber", AppInfo.toCodeNumber(registerCode,phone));
    }
    @RequestMeta(logTitle = "APP信息",logDescription = "获取APP信息")
    public JSONMessage getAppInfo(){
        return new JSONMessage(0).push("model",CacheUtil.get(AppInfo.class,""));
    }
    /*
     * 编辑应用信息
     */
    public VueResult toEdit() throws Exception {
        PowerUtil.verifyPower(PowerUtil.SysData);//权限判断
        model= CacheUtil.get(AppInfo.class,"");
        ModelForm form = new ModelForm("应用信息");
        form.addItem(
            FormInput.input("注册编码", "model.registerCode").setSpan(2).setPlaceholder("请输入注册编码"),
            FormHtml.ins("序列号生成",FormButton.pri("生成注册序列号","toCode")),
            FormInput.input("注册标题", "model.registerTitle").setSpan(2).setPlaceholder("请输入注册标题"),
            FormInput.selectImage("注册图片","model","registerImage","registerImage"),
            FormInput.textarea("注册描述", "model.registerDescription").setSpan(3).setPlaceholder("请输入注册描述"),
            FormInput.input("注册提醒","model.registerAlert").setSpan(3).setPlaceholder("请输入注册提醒"),
            FormInput.input("级别标题", "model.levelTitle").setSpan(2).setPlaceholder("请输入级别标题"),
            FormInput.selectImage("级别图片","model","levelImage","levelImage"),
            FormInput.textarea("级别描述", "model.levelDescription").setSpan(3).setPlaceholder("请输入级别描述"),
            FormInput.input("级别提醒","model.levelAlert").setSpan(3).setPlaceholder("请输入级别提醒"),
            FormInput.input("报价单底部1","model.offerBottom1Txt").setPlaceholder("请输入报价单底部1"),
            FormInput.input("报价单底部2","model.offerBottom2Txt").setPlaceholder("请输入报价单底部2"),
            FormInput.input("底部2链接","model.offerBottom2Link").setPlaceholder("请输入底部2链接"),
            FormInput.input("微信AppId","model.wxAppId").setPlaceholder("请输入微信小程序AppId"),
            FormInput.input("微信Secret","model.wxSecret").setSpan(2).setPlaceholder("请输入微信小程序Secret")
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}"),
            FormEvent.toModal("toCode",actionUrl("toCode"),"{}",500)
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    public VueResult toCode() throws Exception {
        PowerUtil.verifyPower(PowerUtil.SysData);//权限判断
        model= CacheUtil.get(AppInfo.class,"");
        ModelForm form = new ModelForm("生成序列号");
        form.addItem(
            FormInput.input("注册编码", "model.registerCode").setSpan(3).setPlaceholder("请输入注册编码"),
            FormInput.input("联系电话", "phone").setSpan(3).setPlaceholder("请输入注册手机号"),
            FormHtml.inputStyle("生成注册码","{{codeNumber||'点击【生成】按钮生成注册码'}}")
        )
        .addData("model",model)
        .addData("phone","")
        .addData("codeNumber","")
        .addButton(
            FormButton.pri("生成","doBuild")
        )
        .addMethod(
            FormEvent.doJson("doBuild",actionUrl("doBuild"),"{registerCode:this.model.registerCode,phone:this.phone}")
                .fillData("codeNumber")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    public AppInfo getModel() {
        return model;
    }

    public void setModel(AppInfo model) {
        this.model = model;
    }
}
